package com.zj.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.dao.BannerMapper;
import com.zj.dao.ReviewMapper;
import com.zj.model.Banner;
import com.zj.model.Reviews;
import com.zj.restApi.FileUploadApi;
import com.zj.restApi.IdGenratorApi;
import com.zj.web.bean.ResponseResult;
import com.zj.web.bean.ReviewsDTO;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
@Service
@Log
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Reviews> implements ReviewService {
    @Autowired
    private IdGenratorApi idGenratorApi;
    @Autowired
    private FileUploadApi fileUploadApi;
    //添加评价
    @Override
    public ReviewsDTO addReview(ReviewsDTO reviewsDTO) {
        //1.调用 idGenerator 服务 取出id号
        ResponseEntity<ResponseResult<Long>> responseEntity = idGenratorApi.getNextId();  //jdk动态代理对象
        if(responseEntity.getStatusCode()!= HttpStatus.OK || responseEntity.getBody()==null){
            throw new RuntimeException("调用idGenerator服务失败");
        }
        ResponseResult<Long> responseResult = responseEntity.getBody();
        log.info(responseResult.toString());
        if (responseResult == null || responseResult.getCode() != 1) {
            throw new RuntimeException("调用idGenerator服务失败");
        }
        Long id = responseResult.getData();
        reviewsDTO.setReviewId(id);

//        文件上传  利用Feign客户端
        MultipartFile[] reviewImageFiles = reviewsDTO.getReviewImageFiles();
        ResponseEntity<ResponseResult> responseEntity1 = this.fileUploadApi.uploadFile(reviewImageFiles);
        if(responseEntity1.getStatusCode()!= HttpStatus.OK || responseEntity1.getBody()==null){
            throw new RuntimeException("调用fileUpload服务失败");
        }
        ResponseResult responseResult1 = responseEntity1.getBody();
        log.info(responseResult1.toString());
        if (responseResult1 == null || responseResult1.getCode() != 1) {
            throw new RuntimeException("调用fileUpload服务失败");
        }
        List<String> urls=(List<String>) responseResult1.getData();
        //将图片url数组转换为字符串
        String reviewImages = String.join(",", urls);
        reviewsDTO.setReviewImages(reviewImages);
        //2.转换为实体类
        Reviews reviews = new Reviews();
        BeanUtils.copyProperties(reviewsDTO,reviews);
        //3.添加到数据库
        this.baseMapper.insert(reviews);
        return reviewsDTO;
    }

    //根据商品ID查询评价列表
    @Override
    public List<ReviewsDTO> getReviewsByProductId(Long productId) {
        return this.baseMapper.selectReviewsByProductId(productId);
    }

    //根据商品ID和评分查询评价详情
    @Override
    public List<ReviewsDTO> getReviewByIdAndRating(Long productId, Integer rating) {
        return this.baseMapper.getReviewByIdAndRating(productId,rating);
    }
}
